package com.xidian.zhiancloud.controller.equipFacility.baseSetting;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.DeviceType;
import com.xidian.zhiancloud.qo.equipFacility.DeviceTypeQO;
import com.xidian.zhiancloud.service.equipFacility.DeviceTypeService;
import com.xidian.zhiancloud.vo.equipFacility.DeviceTypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;

/**
 * 设备设施 基础设置 设备类型设置接口
 *
 * @Author: 任启亮
 * @Date: 2020/11/12
 */
@RestController
@RequestMapping("/DeviceType")
public class DeviceTypeController extends BaseController {
    @Autowired
    private DeviceTypeService deviceTypeService;

    /**
     * 增加设备类型设置
     *
     * @param deviceType 设备类型设置对象
     * @return 是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> add(DeviceType deviceType) {
        deviceType.setCompanyId(getSessionUserCompanyId());
        deviceType.setCreatorId(getSessionUserid());
        deviceType.setAddTime(new Date(System.currentTimeMillis()));
        Boolean result = deviceTypeService.save(deviceType);
        return ResultDTO.successOf(result, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id获取设备类型设置
     *
     * @param id 设备类型设置对象id
     * @return 设备类型设置对象
     */
    @GetMapping("/getById")
    public ResultDTO<DeviceType> getById(@RequestParam("id") Long id) {
        return ResultDTO.successOf(deviceTypeService.getById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id删除设备类型设置
     *
     * @param ids ids数组
     * @return 是否删除标志
     */
    @PostMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] ids) {
        Boolean result = deviceTypeService.removeByIds(Arrays.asList(ids));
        return ResultDTO.successOf(result, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id修改设备类型设置
     *
     * @param deviceType 设备类型设置对象
     * @return 是否修改状态
     */
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchById(DeviceType deviceType) {
        deviceType.setCompanyId(null);
        deviceType.setCreatorId(null);
        deviceType.setAddTime(null);
        Boolean result = deviceTypeService.updateById(deviceType);
        return ResultDTO.successOf(result, GeneralResponseEnums.SUCCESS);
    }

    /**
     * @param deviceTypeQO 查询参数
     * @return 设备类型设置分页查询
     */
    @GetMapping("/getListByQO")
    public ResultDTO<IPage<DeviceTypeVO>> getList(DeviceTypeQO deviceTypeQO) {
        if (deviceTypeQO.getCurrent()== null || deviceTypeQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        deviceTypeQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(deviceTypeService.selectByQO(deviceTypeQO), GeneralResponseEnums.SUCCESS);
    }
}
